package com.fdb.efp.flow.service.impl;

import static com.fdb.basic.sequence.service.DefaultPatternDescriptor.SEQ;

import java.util.List;
import java.util.Objects;

import com.fdb.efp.flow.service.dao.BizFlowTaskDao;
import com.fdb.efp.flow.service.dao.BizFlowTaskParamDao;
import com.fdb.efp.flow.service.facade.BizFlowTaskService;
import com.fdb.efp.flow.service.vo.BizFlowTaskParamVO;
import com.fdb.efp.flow.service.vo.BizFlowTaskVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fdb.basic.sequence.service.DefaultPatternDescriptor;
import com.fdb.basic.sequence.service.PatternDescriptor;
import com.fdb.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.fdb.efp.flow.common.constance.FlowConstance;
import com.fdb.efp.flow.service.domain.BizFlowTask;
import com.fdb.efp.flow.service.domain.BizFlowTaskParam;
import com.fdb.basic.framework.core.base.AbstractFrameworkService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.SequenceConstance;
import com.fdb.basic.framework.core.util.StringUtil;
/**
 * <p>ServiceImpl: 业务流转任务信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizFlowTaskService")
public class BizFlowTaskServiceImpl extends AbstractFrameworkService implements BizFlowTaskService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizFlowTaskServiceImpl.class);

	@Autowired
	private BizFlowTaskDao bizFlowTaskDao;
	
	@Autowired
	private BizFlowTaskParamDao bizFlowTaskParamDao;
	
	@Autowired
	private PatternedLimitableSeqService sequenceService;
	
	public static final PatternDescriptor PATTERN = 
			DefaultPatternDescriptor.of("T" + SEQ, 6);
	/**
	 * 新增操作
	 */
	public int insertBizFlowTask(BizFlowTaskVO inBizFlowTaskVo) {
		logger.debug("当前新增数据为:"+ inBizFlowTaskVo.toString());
		int num = 0;
		try {
			BizFlowTask bizFlowTask = new BizFlowTask();
			inBizFlowTaskVo.setTaskId
			(sequenceService.getPatternedSequence(SequenceConstance.BIZ_FLOW_TASK_SEQ_ID, PATTERN));
			beanCopy(inBizFlowTaskVo, bizFlowTask);
			num = bizFlowTaskDao.insertBizFlowTask(bizFlowTask);
			
			//插入插件参数
			if(inBizFlowTaskVo != null) {
				List<BizFlowTaskParamVO> paramList = inBizFlowTaskVo.getParamData();
				if(paramList != null) {
					for(int i=0; i<paramList.size(); i++) {
						BizFlowTaskParamVO vo = paramList.get(i);
						BizFlowTaskParam taskParam = new BizFlowTaskParam();
						beanCopy(vo, taskParam);
						taskParam.setTaskId(bizFlowTask.getTaskId());
						bizFlowTaskParamDao.insertBizFlowTaskParam(taskParam);
					}
				}
			}
			
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizFlowTaskVO inBizFlowTaskVo) {
		logger.debug("当前删除数据条件为:"+ inBizFlowTaskVo);
		int num = 0;
		try {
			BizFlowTask bizFlowTask = new BizFlowTask();
			beanCopy(inBizFlowTaskVo, bizFlowTask);
			num = bizFlowTaskDao.deleteByPk(bizFlowTask);
			//删除任务参数
			bizFlowTaskParamDao.deleteByTaskId(bizFlowTask.getTaskId());
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowTaskVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizFlowTaskVO inBizFlowTaskVo) {
		logger.debug("当前修改数据为:"+ inBizFlowTaskVo.toString());
		int num = 0;
		try {
			BizFlowTask bizFlowTask = new BizFlowTask();
			beanCopy(inBizFlowTaskVo, bizFlowTask);
			num = bizFlowTaskDao.updateByPk(bizFlowTask);
			
			//插入插件参数
			if(inBizFlowTaskVo != null) {
				List<BizFlowTaskParamVO> paramList = inBizFlowTaskVo.getParamData();
				if(paramList != null) {
					for(int i=0; i<paramList.size(); i++) {
						BizFlowTaskParamVO vo = paramList.get(i);
						if(!StringUtil.isEmpty(vo.getOpFlag())) {
							
							BizFlowTaskParam taskParam = new BizFlowTaskParam();
							beanCopy(vo, taskParam);
							taskParam.setTaskId(bizFlowTask.getTaskId());
							
							if(vo.getOpFlag().equals(FlowConstance.BIZ_OPT_FLAG_ADD)) {
								bizFlowTaskParamDao.insertBizFlowTaskParam(taskParam);
							} else if(vo.getOpFlag().equals(FlowConstance.BIZ_OPT_FLAG_UPDATE)) {
								bizFlowTaskParamDao.updateByPk(taskParam);
							} else if(vo.getOpFlag().equals(FlowConstance.BIZ_OPT_FLAG_DELETE)) {
								bizFlowTaskParamDao.deleteByPk(taskParam);
							}
						}
					}
				}
			}
			
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowTaskVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizFlowTaskVO queryByPk(BizFlowTaskVO inBizFlowTaskVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizFlowTaskVo);
		try {
			BizFlowTask queryBizFlowTask = new BizFlowTask();
			beanCopy(inBizFlowTaskVo, queryBizFlowTask);
			BizFlowTask queryRslBizFlowTask = bizFlowTaskDao.queryByPk(queryBizFlowTask);
			if (Objects.nonNull(queryRslBizFlowTask)) {
				BizFlowTaskVO outBizFlowTaskVo = beanCopy(queryRslBizFlowTask, new BizFlowTaskVO());
				logger.debug("当前查询结果为:"+ outBizFlowTaskVo.toString());
				return outBizFlowTaskVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowTaskVO> queryAllOwner(BizFlowTaskVO bizFlowTaskVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizFlowTaskVO> list = null;
		try {
			List<BizFlowTask> bizFlowTasks = bizFlowTaskDao.queryAllOwnerByPage(bizFlowTaskVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizFlowTasks.size());
			pageSet(bizFlowTasks, bizFlowTaskVo);
			list = (List<BizFlowTaskVO>) beansCopy(bizFlowTasks, BizFlowTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowTaskVO> queryAllCurrOrg(BizFlowTaskVO bizFlowTaskVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizFlowTask> bizFlowTasks = bizFlowTaskDao.queryAllCurrOrgByPage(bizFlowTaskVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizFlowTasks.size());
		List<BizFlowTaskVO> list = null;
		try {
			pageSet(bizFlowTasks, bizFlowTaskVo);
			list = (List<BizFlowTaskVO>) beansCopy(bizFlowTasks, BizFlowTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowTaskVO> queryAllCurrDownOrg(BizFlowTaskVO bizFlowTaskVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizFlowTask> bizFlowTasks = bizFlowTaskDao.queryAllCurrDownOrgByPage(bizFlowTaskVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizFlowTasks.size());
		List<BizFlowTaskVO> list = null;
		try {
			pageSet(bizFlowTasks, bizFlowTaskVo);
			list = (List<BizFlowTaskVO>) beansCopy(bizFlowTasks, BizFlowTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	/**
	 * 查询可选的前置任务
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowTaskVO> queryEnableSelectPreTaskByPage(BizFlowTaskVO bizFlowTaskVo) {
		List<BizFlowTaskVO> list = null;
		try {
			List<BizFlowTask> bizFlowTasks = bizFlowTaskDao.queryEnableSelectPreTaskByPage(bizFlowTaskVo);
			pageSet(bizFlowTasks, bizFlowTaskVo);
			list = (List<BizFlowTaskVO>) beansCopy(bizFlowTasks, BizFlowTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	/**
	 * 删除节点下的所有任务
	 */
	public int deleteByBizNodeId(BizFlowTaskVO inBizFlowTaskVo) {
		logger.debug("当前删除数据条件为:"+ inBizFlowTaskVo);
		int num = 0;
		try {
			BizFlowTask bizFlowTask = new BizFlowTask();
			beanCopy(inBizFlowTaskVo, bizFlowTask);
			num = bizFlowTaskDao.deleteByBizNodeId(bizFlowTask);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowTaskVo +"删除的数据条数为"+ num);
		return num;
	}

}
